package org.zjut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zjut.db.LearnerCourseAttrDataBase;
import org.zjut.db.RcommondDataBase;
import org.zjut.util.CommonUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Rising
 * @date 2017/9/21
 * @des 学习者对课程的属性
 */
@Controller
public class LearnerCourseAttrController {
    @Autowired
    LearnerCourseAttrDataBase mLearnerCourseAttrDataBase;
    @Autowired
    RcommondDataBase mRecommondDataBase;

    /**
     * 新增学习者与课程之间的属性关系
     * @return
     */
    @RequestMapping(value = "/addUserCourseAttr", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addCourseAttr(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));

        }
        try {
            return JSON.toJSONString(mLearnerCourseAttrDataBase.addCourseAttr(params));
        } catch (Exception e) {
            return JSON.toJSONString(CommonUtil.setErrorInfo("生成失败"));
        }
    }
    /**
     * 获取学习者与课程之间的属性关系
     * @return
     */
    @RequestMapping(value = "/getUserCourseAttr", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getCourseAttr(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));

        }
        return JSON.toJSONString(mLearnerCourseAttrDataBase.getCourseAttr(params));
    }
    /**
     * 删除学习者与课程之间的属性关系
     * @return
     */
    @RequestMapping(value = "/delUserCourseAttr", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delCourseAttr(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));

        }
        return JSON.toJSONString(mLearnerCourseAttrDataBase.delCourseAttr(params));
    }

    /**
     * 获取学习者与课程之间的属性关系
     * @return
     */
    @RequestMapping(value = "/getRecommonKnowledge", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getRecommonKnowledge(HttpServletRequest request){
        JSONObject params =null;
        if(request==null){
            return JSON.toJSONString(CommonUtil.setErrorInfo("请求参数有问题，请注意检查"));
        }else{
            params = JSON.parseObject(CommonUtil.getBodyRequest(request));
        }
        return JSON.toJSONString(mRecommondDataBase.getRecommonKnowledge(params));
    }
}
